package cn.jinjimi.app.main.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import bolts.Continuation;
import bolts.Task;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.domain.data.dto.TokenDto;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.main.widget.MTextWatcher;
import cn.jinjimi.app.main.activity.LoginActivity;
import cn.jinjimi.app.main.extend.BaseFragment;

/**
 *
 * @author zhenhua
 */
public class RegisterPswFragment extends BaseFragment implements View.OnClickListener {
    private static final String TAG = "RegisterPswFragment";
    private String mobile, captcha;
    private EditText psw, psw1;
    private Button cleanPsw;
    private Button cleanPsw1;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_register_psw;
    }

    @Override
    protected void initial() {
        Bundle bundle = getArguments();
        mobile = bundle.getString(BundleKey.PHONE);
        captcha = bundle.getString(BundleKey.CAPTCHA);

        psw = (EditText) findView().findViewById(R.id.et_password);
        cleanPsw = (Button) findView().findViewById(R.id.bt_clean_password);
        psw1 = (EditText) findView().findViewById(R.id.et_password_again);
        cleanPsw1 = (Button) findView().findViewById(R.id.bt_clean_password_again);
        TextView btnSubmit = findView().findViewById(R.id.bt_submit);
        btnSubmit.setOnClickListener(this);
        cleanPsw.setOnClickListener(this);
        cleanPsw1.setOnClickListener(this);
        psw.addTextChangedListener(new MTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    cleanPsw.setVisibility(View.INVISIBLE);
                } else {
                    cleanPsw.setVisibility(View.VISIBLE);
                }
            }
        });
        psw1.addTextChangedListener(new MTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    cleanPsw1.setVisibility(View.INVISIBLE);
                } else {
                    cleanPsw1.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    public static RegisterPswFragment newInstance(String text, String captcha) {
        RegisterPswFragment fragment = new RegisterPswFragment();
        Bundle args = new Bundle();
        args.putString(BundleKey.PHONE, text);
        args.putString(BundleKey.CAPTCHA, captcha);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_clean_password:
                psw.setText("");
                break;
            case R.id.bt_clean_password_again:
                psw1.setText("");
                break;
            case R.id.bt_submit:
                submit();
                break;
            default:break;
        }

    }

    private boolean checkPasswordInput(String pwd) {
        if (StringUtils.isEmpty(pwd)) {
            ToastUtils.toastMessage(getActivity(), R.string.input_u_password);
            return false;
        }
        return true;
    }

    private boolean checkPhoneInput(String mobile) {
        if (TextUtils.isEmpty(mobile)) {
            ToastUtils.toastMessage(getActivity(), cn.jinjimi.base.R.string.empty_phone);
            return false;
        }
        if (!StringUtils.isMobile(mobile)) {
            ToastUtils.toastMessage(getActivity(), cn.jinjimi.base.R.string.input_u_correct_phone);
        }
        return true;
    }

    private boolean checkCaptchaInput(String captcha) {
        if (TextUtils.isEmpty(captcha)) {
            ToastUtils.toastMessage(getActivity(), R.string.account_check_captcha);
            return false;
        }
        return true;
    }

    /**
     * 提交请求
     */
    private void submit() {
        String pwdStr = psw.getText().toString();
        String pwdStr1 = psw1.getText().toString();
        if (!TextUtils.equals(pwdStr, pwdStr1)) {
            ToastUtils.toastMessage(getActivity(), R.string.psw_not_same);
            return;
        }

        if (!checkPasswordInput(pwdStr)) {
            return;
        }

        if (!checkPhoneInput(mobile)) {
            return;
        }

        if (!checkCaptchaInput(captcha)) {
            return;
        }

        ((LoginActivity) getActivity()).showLoadingIndicator();
        UserController
                .create(mobile, pwdStr, mobile, captcha)
                .onSuccess(new Continuation<TokenDto, Void>() {
                    @Override
                    public Void then(Task<TokenDto> task) throws Exception {
                        ((LoginActivity) getActivity()).hideLoadingIndicator();
                        if (task.isCompleted()) {
                            TokenDto dto = task.getResult();
                            if (!TextUtils.isEmpty(dto.token)) {
                                ToastUtils.toastMessage(getActivity(), R.string.account_create_success);
                                getActivity().finish();
                            }
                        }
                        return null;
                    }
                }, Task.UI_THREAD_EXECUTOR);
    }


    @Override
    public void onPause() {
        super.onPause();
        AndroidUtils.hideSoftKeyBoard(getActivity(), psw);
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(getResources().getString(R.string.register_all));
    }
}
